<?php
require_once("../../../util/configuration/configuration_parametters.php");
require_once("../../../util/" . MESSAGES_PROPERTIES . "messages_model.php");
require_once("../../../util/exception/exception_handler.php");

require_once("../../article_ping/to/article_ping_to.php");
require_once("../../article_ping/dao/article_ping_dao_factory.php");

require_once("../../article/to/article_to.php");
require_once("../../article/dao/article_dao_factory.php");

require_once("../../autor/to/autor_to.php");
require_once("../../autor/dao/autor_dao_factory.php");


class Article_ping_dao_test {

	private $dao;
	private $autor;
	private $article;
	
	public function __construct() {}
	
	public function before_test() {			
		$this->autor = (object) Autor_dao_factory::singleton()->get_dao()->create(new Autor_to('xxxxxxxxxxxx','password','email'));		
		$date = date("Y-m-d H:i:s");
		$this->article = (object) Article_dao_factory::singleton()->get_dao()->create(new Article_to(0, 'title', $this->autor->get_login(), $date, 'open', 0));
			
		$this->dao = Article_ping_dao_factory::singleton()->get_dao();
	}
	
	public function run_test() {
		try {
			$date = date("Y-m-d H:i:s");
			$article_ping = new Article_ping_to($this->article->get_number(), 
													$this->autor->get_login(), 'comment', 
													'positive', $date, 'open');
			
			$article_ping = (object) $this->dao->create($article_ping);		
			echo 'create<br/>';			

			echo 'article_ping: '; 
			echo $article_ping->to_string();
			echo '<br/>';
			
			if ($this->dao->exist($article_ping->get_to_article_number(), 
									$article_ping->get_from_autor_login())) {
				
				$result = $this->dao->find_by_article_and_autor_login($article_ping->get_to_article_number(), 
																		$article_ping->get_from_autor_login());
				echo 'find<br/>';
				
				$aux_article_ping = new Article_ping_to($result['to_article_number'], $result['from_autor_login'], 
																	$result['comment'],	$result['type'], $result['date'], $result['state']);
				if ($article_ping->equals($aux_article_ping)) {
					$article_ping->set_state('wait');
					$this->dao->update($article_ping);
					echo 'update<br/>';
					
					$result = $this->dao->find_by_article_and_autor_login($article_ping->get_to_article_number(), 
																		$article_ping->get_from_autor_login());
					$aux_article_ping = new Article_ping_to($result['to_article_number'], $result['from_autor_login'], 
																	$result['comment'],	$result['type'], $result['date'], $result['state']);
					echo 'article_ping: ';
					echo $aux_article_ping->to_string();
					echo '<br/>';		
				}	
									
				$this->dao->remove($article_ping->get_to_article_number(), 
									$article_ping->get_from_autor_login());
				echo 'remove<br/>';
				
			}
		}
		catch (Exception $e) {
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function after_test() {
		Article_dao_factory::singleton()->get_dao()->remove($this->article->get_number());
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor->get_login());
	}
}

//test
$test = new Article_ping_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();

?>